<?php
  class PostMapper extends Mapper{
    const TBL_NAME = 'post';
    
    public function save(PostModel $post){
      $st = $this->db->prepare('INSERT INTO '.self::TBL_NAME.' (author_id, header, post_text)
                                VALUES (:aid, :h, :txt );');
      $st->bindValue(':aid', $post->get_AuthorId());
      $st->bindValue(':h', $post->get_header());
      $st->bindValue(':txt', $post->get_body());
      $st->execute();
      return $this->db->lastInsertId();
    }
    
    public function update(PostModel $post){
      $st = $this->db->prepare('UPDATE '.self::TBL_NAME.' 
                                SET
                                  header=:h, post_text=:txt
                                WHERE id = :id;');
      $st->bindValue(':h', $post->get_header());
      $st->bindValue(':txt', $post->get_body());
      $st->bindValue(':id', $post->get_id(), PDO::PARAM_INT);
      $st->execute();
    }

    public function getNPosts($offset, $quant){
      $postsList = array();
      
      $st = $this->db->prepare('SELECT 
                                  id,
                                  header,
                                  post_text as body,
                                  author_id as authorId,
                                  created_on as createdOn
                                  FROM '.self::TBL_NAME.'
                                  ORDER by id DESC
                                  LIMIT :offset, :quant;');
      $st->bindValue(':offset', $offset, PDO::PARAM_INT);
      $st->bindValue(':quant', $quant, PDO::PARAM_INT);
      $st->execute();
      $postsDataList = $st->fetchAll(PDO::FETCH_ASSOC);

      foreach ($postsDataList as $pd) {
        $postsList[] = new PostModel($pd);
      }
      return $postsList;

    }
    public function getPostById($id){  
      $st = $this->db->prepare('SELECT 
                                  id,
                                  header,
                                  post_text as body,
                                  author_id as authorId,
                                  created_on as createdOn
                                  FROM '.self::TBL_NAME.'
                                  WHERE id=:id');
      $st->bindValue(':id', $id, PDO::PARAM_INT);
      $st->execute();
      
      $postData = $st->fetch(PDO::FETCH_ASSOC);

      $post = new PostModel($postData);
      return $post;
    }
    public function UnsafeGetPostById($id){  
      $st = $this->db->prepare('SELECT 
                                  id,
                                  header,
                                  post_text as body,
                                  author_id as authorId,
                                  created_on as createdOn
                                  FROM '.self::TBL_NAME.'
                                  WHERE id='.$id);
      // var_dump($st);
      $st->execute();
      // var_dump($st->errorInfo());
      // var_dump($st->fetchAll(PDO::FETCH_ASSOC));
      $postData = $st->fetch(PDO::FETCH_ASSOC);

      $post = new PostModel($postData);
      return $post;
    }
  }

?>